package com.cms.stu.service.impl;

import com.cms.stu.dao.StudentDao;
import com.cms.stu.entity.MajorEntity;
import com.cms.stu.entity.StudentEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cms.common.utils.PageUtils;
import com.cms.common.utils.Query;

import com.cms.stu.dao.ClassDao;
import com.cms.stu.entity.ClassEntity;
import com.cms.stu.service.ClassService;

import javax.annotation.Resource;


@Service("classService")
public class ClassServiceImpl extends ServiceImpl<ClassDao, ClassEntity> implements ClassService {

    @Resource
    private StudentDao studentDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<ClassEntity> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(key)){
            wrapper.like("name", key).or().eq("id", key).or().eq("major_id", key);
        }
        IPage<ClassEntity> page = this.page(
                new Query<ClassEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<ClassEntity> listByGrade(String grade) {
        return baseMapper.listByGrade(Integer.parseInt(grade));
    }

    @Override
    public List<StudentEntity> studentByClassAndNew() {
        QueryWrapper<ClassEntity> wrapper = new QueryWrapper<>();
        wrapper.last("and year(now()) = level");
        List<ClassEntity> classEntities = baseMapper.selectList(wrapper);
        List<Long> classIds = classEntities.stream().map(item -> {
            return item.getId();
        }).collect(Collectors.toList());
        QueryWrapper<StudentEntity> wrapper1 = new QueryWrapper<>();
        wrapper1.in("class_id", classIds);
        List<StudentEntity> studentEntities = studentDao.selectList(wrapper1);
        return studentEntities;
    }

}